#!/usr/bin/php
<?php
/***************************************************************************
 *
 *   copyright            : (C) 2011 Winds of Storm
 *
 *   $Id$
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

require_once(dirname(__FILE__) . "/../config.php");
$skip_header    = 1;
require_once($libdir . "init." . $phpEx);
require_once($libdir . "chat_functs." . $phpEx);
require_once($libdir . "emailer." . $phpEx);
require_once($libdir . "transcripts." . $phpEx);

if (!isset($config['TS_SCHEDULED']) || $config['TS_SCHEDULED'] != 'enabled') {
    print("Transcript scheduling not enabled, aborting.\n");
    exit;
}

$board_config   = array(
    'default_lang'  => '',
    'sendmail_fix'  => 1
    );

$today      = mktime(0, 0, 0,   date("m"),    date("d"),     date("Y"));
$yesterday  = mktime(0, 0, 0,   date("m"),    date("d")-1,   date("Y"));
$start      = date("'Y-m-j 00:00:00'", $yesterday);
$end        = date("'Y-m-j 00:00:59'", $today);
$rid        = -1;
$filter     = 'base';
if (isset($config['TS_DISPLAY_PMS']) && $config['TS_DISPLAY_PMS'] == 'enabled') {
    $filter = 'admin';
}

$tpl    = new Template($config['TPL_DIR']);
$tpl->set_filenames(array('body' => 'email/ts_results.tpl'));
$events = pull_transcript_events($start, $end, $rid, $filter);
if ($events == '') {
    $events = array();
}
foreach ($events as $row) {
    $date_fmt   = "[" . $row['event_date_fmt'] . "] ";
    $room_name  = '';
    if ($rid == -1) {
        if ($row['room_id_to'] > 0) {
            $room_name  = "{$row['room_name']}: ";
        } else {
            if ($row['user_id_to'] > 0) {
                $from_name  = getUserAliasById($row['user_id_from']);
                $to_name    = getUserAliasById($row['user_id_to']);
                $room_name  = "Private Message from $from_name to $to_name: ";
            } else {
                $room_name  = 'Serverwide: ';
            }
        }
    }

    $tpl->assign_block_vars('events', array(
        'ROOM_NAME'     => $room_name,
        'EVENT_ID'      => $row['event_id'],
        'EVENT_DATE'    => $date_fmt,
        'EVENT_TEXT'    => stripslashes($row['event_text'])
    ));
}

ob_start();
$tpl->pparse('body');
$etext  = ob_get_contents();
ob_end_clean();

$emailer = new emailer(false);
$emailer->from($config['TS_EMAIL_FROM']);
$emailer->replyto($config['TS_EMAIL_FROM']);
$emailer->use_template('email/daily_transcripts.tpl', $config['TPL_DIR']);
foreach (explode(',', $config['TS_EMAIL_LIST']) as $email) {
    print("Adding $email to delivery list...\n");
    $emailer->cc($email);
}
$emailer->assign_vars(array(
	'FORUM_NAME'	=> $config['FORUM_NAME'],
    'EVENTS'        => $etext,
	));

$emailer->send();
$emailer->reset();

?>
